import torch

# 笔记.md
import torchvision.datasets as dataset  # 内置了一些公开数据集
import torchvision.transforms as transforms  # 数据处理，数据增强库

test_data = dataset.MNIST(root='../笔记.md/mnist',
                          train=False,
                          transform=transforms.ToTensor(),
                          download=True)

# batch_size 取出一部分来训练，而不是全丢入
import torch.utils.data as data_utils

test_loader = data_utils.DataLoader(dataset=test_data,
                                    batch_size=64,
                                    shuffle=True)  # 打乱


# net 一般会单独定义一个文件中，完成代码的复用
class CNN(torch.nn.Module):

    def __init__(self):
        super(CNN, self).__init__()
        # 通过序列工具来构造网络
        self.conv = torch.nn.Sequential(
            torch.nn.Conv2d(1, 32, kernel_size=5, padding=2),
            torch.nn.BatchNorm2d(32),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2)
        )
        self.fc = torch.nn.Linear(14 * 14 * 32, 10)

    def forward(self, x):
        out = self.conv(x)
        out = out.view(out.size()[0], -1)
        out = self.fc(out)
        return out


cnn = torch.load('../model/model_k2.pkl')
cnn = cnn.cuda()

# loss
loss_func = torch.nn.CrossEntropyLoss()  # 交叉熵损失函数

# optimizer
optimizer = torch.optim.Adam(cnn.parameters(), lr=0.01)

# test
import cv2

loss_test = 0
accuracy_test = 0
for i, (images, labels) in enumerate(test_loader):
    images = images.cuda()
    labels = labels.cuda()

    outputs = cnn(images)

    loss_test += loss_func(outputs, labels)
    _, pred = outputs.max(1)
    accuracy_test += (pred == labels).sum().item()

    # 转移到CPU上
    images = images.cpu().numpy()
    labels = labels.cpu().numpy()
    pred = pred.cpu().numpy()

    # batchsize * 1 * 28 * 28
    for idx in range(images.shape[0]):
        im_data = images[idx]
        im_label = labels[idx]
        im_pred = pred[idx]

        # 将通道维度放到最后面
        im_data = im_data.transpose(1, 2, 0)

        print('label', im_label)
        print('pred', im_pred)
        cv2.imshow('imdata', im_data)
        cv2.waitKey(0)

accuracy_test = accuracy_test / len(test_data)
loss_test = loss_test / (len(test_data) // 64)

print('accuracy is {}, loss_test is {}'.format(accuracy_test, loss_test.item()))
